<?php

/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/9/12
 * Time: 13:48
 */
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Symfony\Component\Console\Input\InputArgument;

class TestCommand extends Command
{
    /**
     * 命令行执行命令
     * @var string
     */
    protected $signature = 'test_command';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->start();
    }

    public function fire()
    {
        $arg = $this->argument('action');
        switch ($arg) {
            case 'start' :
                $this->info('swoole observer started');
                $this->start();
                break;
            case 'stop' :
                $this->info('stopped');
                break;
            case 'restart' :
                $this->info('restart');
                break;
        }
    }

    private function start()
    {
        $this->serv = at_swoole_server('0.0.0.0', 9751);
        $this->serv->set([
            'work_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode' => 1
        ]);
        $handler = App::make('handlers\SwooleHandler');
        $this->serv->on('Start', [$handler, 'onStart']);
        $this->serv->on('Connect', [$handler, 'onConnect']);
        $this->serv->on('Receive', [$handler, 'onReceive']);
        $this->serv->on('Close', [$handler, 'onClose']);

        $this->serv->start();
    }

    protected function getArguments()
    {
        return [
            [
                'action', InputArgument::REQUIRED, 'start|stop|restart'
            ]
        ];
    }
}